package org.jwcloud.project.module.zx.template.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.template.service.RptTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle",
description = "任务模板信息", tags = "task")
@RestController
@RequestMapping("/rest/rptTask")
public class RptTaskController extends BaseController{
	
//	@Autowired
//    private CommonService commonService;
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
    private RptTemplateService rptTemplateService;

    
    
	private Resp<List<Map<String, String>>> quertCodeList(String tableName) {
		// TODO Auto-generated method stub
		return Resp.success(rptCommonRepository.queryCodeList(new HashMap() {{this.put("tableName", tableName);}}));
	}
	
	@PostMapping("changeTaskIndex")
    @ApiOperation(value = "修改任务顺序", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "qian_id", value = "交换的id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "hou_id", value = "交换的id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "qian_idx", value = "交换后的序号", required = true, dataType = "int", paramType = "query"),
    	@ApiImplicitParam(name = "hou_idx", value = "交换后的序号", required = true, dataType = "int", paramType = "query"),
    })
    public Resp changeTaskIndex(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "qian_id", "交换的id 前"
				, "hou_id", "交换的id 后"
				, "qian_idx", "交换的序号 前"
				, "hou_idx", "交换的序号 后");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.changeTaskIndex(param);
    }
	
	@PostMapping("updateTask")
    @ApiOperation(value = "修改任务", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "duty_dept", value = "责任部门", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dur_time", value = "任务时长", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sp_person", value = "审批人 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_person_name", value = "审批人名字", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_level", value = "审批层级", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept", value = "审批人部门 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept_name", value = "审批人部门名字", required = false, dataType = "string", paramType = "query"),
    })
    public Resp updateTask(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.updateTask(param);
    }
	
	@PostMapping("updateItem_sz")
    @ApiOperation(value = "修改要件设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "要件id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "要件名", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_code", value = "要件编码", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_content", value = "要件描述", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "upload_type", value = "上传方式", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "col_name", value = "字段名称 aa;ss;dd", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "col_type", value = "字段类型 aa;ss;dd", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "is_mvp", value = "是否关键要件 1是 0不是", required = true, dataType = "string", paramType = "query"),
    })
    public Resp updateItem_sz(
    		@RequestBody(required = false) Map<String,Object> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "要件id"
				, "item_code", "要件编码"
				, "item_name", "要件名");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.updateItem_sz(param);
    }
	
	
	@GetMapping("queryTaskList")
    @ApiOperation(value = "任务列表", notes = "主列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "rel_work_id", value = "工作id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryTaskList(
    		@RequestParam(required = false) Map<String,String> param
    		){
//		if(StringUtils.isEmpty(param.get("rel_work_id")))
//			return Resp.error("参数不可为空");
		String flag = FmtUtil.nullRes(param, "rel_work_id", "工作id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.queryTaskList(param);
    }
	
	@GetMapping("queryTaskList_sz")
    @ApiOperation(value = "任务列表 设置", notes = "主列表  分页")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_code", value = "task_code", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "task_name", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryTaskList_sz(
    		Pagination pagination, 
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryTaskList_sz(pagination, param);
    }
	
	@GetMapping("queryTaskList_sz_unpagi")
    @ApiOperation(value = "任务列表 不分页", notes = "主列表  不分页")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_code", value = "task_code", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "task_name", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryTaskList_sz_unpagi(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryTaskList_sz_unpagi(param);
    }
	
	@GetMapping("queryItemList_sz")
    @ApiOperation(value = "要件 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "item_name", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryItemList_sz(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryItemList_sz(param);
    }
	
	@GetMapping("queryItemList_sz_pagi")
    @ApiOperation(value = "要件 设置  分页", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "item_name", value = "item_name", required = false, dataType = "string", paramType = "query"),
    })
    public Resp<Response<PageListSlice>> queryItemList_sz_pagi(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryItemList_sz(pagination, param);
    }
	
	@PostMapping("insertTask")
    @ApiOperation(value = "添加任务", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "rel_work_id", value = "工作id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_sz", value = "任务设置id", required = true, dataType = "string", paramType = "query"),
    	//@ApiImplicitParam(name = "task_code", value = "任务编码", required = true, dataType = "string", paramType = "query"),
    	//@ApiImplicitParam(name = "task_name", value = "任务名字", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dur_time", value = "时长", required = true, dataType = "int", paramType = "query"),
    	@ApiImplicitParam(name = "duty_dept", value = "责任部门", required = true, dataType = "string", paramType = "query"),
    	
    	
    	@ApiImplicitParam(name = "sp_person_id", value = "审批人 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_person_name", value = "审批人名字", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_level", value = "审批层级", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_dept_id", value = "审批人部门 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_dept_name", value = "审批人部门名字", required = false, dataType = "string", paramType = "query"),
    	
    	//@ApiImplicitParam(name = "item_name", value = "要件", required = false, dataType = "string", paramType = "query"),
    	//@ApiImplicitParam(name = "item_content", value = "要件", required = false, dataType = "string", paramType = "query"),
    })
    public Resp insertTask(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param, "rel_work_id", "工作id", "rel_sz", "任务设置id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.insertTask(param);
    }
	
	@PostMapping("insertTask_sz")
    @ApiOperation(value = "添加任务 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "task_code", value = "任务编码", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名字", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "upload_type", value = "上传类型 aa;ss;dd", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dur_time", value = "时长", required = true, dataType = "int", paramType = "query"),
    	
    	@ApiImplicitParam(name = "item_code", value = "要件id aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	//@ApiImplicitParam(name = "item_name", value = "要件 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	//@ApiImplicitParam(name = "item_content", value = "要件 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    })
    public Resp insertTask_sz(
    		@RequestBody(required = false) Map<String,Object> param
    		){
//		System.out.println(param);
		String flag = FmtUtil.nullRes(param
				, "task_code", "任务编码"
				, "task_name", "任务名字"
				, "item_code", "要件名称"
				, "upload_type", "上报类型");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.insertTask_sz(param);
    }
	
	@PostMapping("updateTask_sz")
    @ApiOperation(value = "修改任务 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id任务task", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "task_code", value = "任务编码", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名字", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "upload_type", value = "上传类型 aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dur_time", value = "时长", required = true, dataType = "int", paramType = "query"),
    	
    	@ApiImplicitParam(name = "item_code", value = "要件id aa;ss;dd", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp updateTask_sz(
    		@RequestBody(required = false) Map<String,Object> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "task_code", "任务编码"
				, "task_name", "任务名字"
				//, "dur_time", "时长"
				);
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.updateTask_sz(param);
    }
	
//	@PostMapping("insertItem")
//    @ApiOperation(value = "添加要件", notes = "")
//    @ApiImplicitParams({
//    	@ApiImplicitParam(name = "task_id", value = "工作", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "item_name", value = "任务编码", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "item_content", value = "上传类型", required = false, dataType = "string", paramType = "query"),
//    })
//    public Object insertItem(
//    		@RequestBody(required = false) Map<String,String> param
//    		){
//        return rptTemplateService.insertItem(param);
//    }
//	
	@PostMapping("insertItem_sz")
    @ApiOperation(value = "添加要件设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "upload_type", value = "上传方式", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_code", value = "要件编号", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "col_name", value = "字段名称  ;分割", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "col_type", value = "字段类型  ;分割", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "is_mvp", value = "是否关键要件 1是 0不是", required = false, dataType = "string", paramType = "query"),
    })
    public Resp insertItem_sz(
    		@RequestBody(required = false) Map<String,Object> param
    		){
		String flag = FmtUtil.nullRes(param
				, "item_code", "要件编号"
				, "item_name", "要件名称"
				, "upload_type", "上传方式"
				, "is_mvp", "是否关键要件");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.insertItem_sz(param);
    }
    
	@DeleteMapping("deleteTask")
    @ApiOperation(value = "删除任务", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteTask(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.deleteTask(param);
    }
	
	@DeleteMapping("deleteTask_bat")
    @ApiOperation(value = "批量删除任务", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id 1;2;3;4", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteTask_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.deleteTask(map);
		}
		
        return Resp.success(1);
    }
	
	
	@DeleteMapping("deleteTask_sz")
    @ApiOperation(value = "删除任务 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteTask_sz(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.deleteTask_sz(param);
    }
	
	@DeleteMapping("deleteTask_sz_bat")
    @ApiOperation(value = "删除任务 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id 1;2;3;4", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteTask_sz_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.deleteTask_sz(map);
		}
		
        return Resp.success(1);
    }
	
	@DeleteMapping("deleteItem")
    @ApiOperation(value = "删除要件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteItem(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        return rptTemplateService.deleteItem(param);
    }
	
	
	@DeleteMapping("deleteItem_bat")
    @ApiOperation(value = "批量删除要件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id 1;2;3;4", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteItem_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.deleteItem(map);
		}
		
        return Resp.success(1);
    }
	
	
	@DeleteMapping("deleteItem_sz")
    @ApiOperation(value = "删除要件 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteItem_sz(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return rptTemplateService.deleteItem_sz(param);
    }
	
	
	
	@DeleteMapping("deleteItem_sz_bat")
    @ApiOperation(value = "批量删除要件 设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp deleteItem_sz_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.deleteItem_sz(map);
		}
		
        return Resp.success(1);
    }
	
	
	@GetMapping("queryTaskList_sz_rel_template")
    @ApiOperation(value = "任务关联模板", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "任务设置id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryTaskList_sz_rel_template(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryTaskList_sz_rel_template(param);
    }
	
	
}